package com.tools.module.app.web;

import cn.hutool.core.lang.UUID;
import com.tools.common.constant.SystemConstant;
import com.tools.common.model.Result;
import com.tools.module.app.util.AliYunUtils;
import com.tools.module.app.util.BaiDuUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 人工智能
 */
@Api(tags ="人工智能")
@RestController
@RequestMapping("app/ai")
public class AiController {

    @Autowired
    private BaiDuUtils baiDuUtils;

    @Autowired
    private AliYunUtils aliYunUtils;
    /**
     * 文字转语音
     */
    @PostMapping("/text2Voice")
    public Result text2Voice(String text,Boolean per){
        String voice = baiDuUtils.text2Voice(text,per);
        return Result.ok("/file/"+voice);
    }

    /**
     * 文字转语音
     */
    @PostMapping("/aliVoice")
    public Result aliVoice(String text,String per){
        // 采用RFC 3986规范进行urlencode编码。
        String textUrlEncode = text;
        try {
            textUrlEncode = URLEncoder.encode(textUrlEncode, "UTF-8")
                    .replace("+", "%20")
                    .replace("*", "%2A")
                    .replace("%7E", "~");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String ali = "E:\\\\file\\\\voice\\\\" + SystemConstant.SF_FILE_SEPARATOR+UUID.randomUUID()+".mp3";
        String format = "mp3";
        int sampleRate = 16000;
        String voice = aliYunUtils.processGETRequet(textUrlEncode, ali, format, sampleRate, per);
        return Result.ok(voice);
    }
}
